package com.ytjj.qmyx.supplychain.api.controller;

import com.alibaba.fastjson.JSONObject;
import com.ytjj.qmyx.supplychain.common.api.CommonPage;
import com.ytjj.qmyx.supplychain.common.api.CommonResult;
import com.ytjj.qmyx.supplychain.common.model.request.ReissueLogisticsRequest;
import com.ytjj.qmyx.supplychain.common.model.request.ReissueLogisticsUpdateRequest;
import com.ytjj.qmyx.supplychain.common.model.response.ReissueLogisticsResponse;
import com.ytjj.qmyx.supplychain.service.BankerService;
import com.ytjj.qmyx.supplychain.service.ReissueLogisticsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 补单管理(供应链)
 * @Version: 1.0
 */
@RestController
@RequestMapping("/reissueLogistics")
@Slf4j
public class ReissueLogisticsController {

    @Resource
    private ReissueLogisticsService reissueLogisticsService;

    /**
     * 供应链补单列表
     * @param reissueLogisticsRequest
     * @return
     */
    @PostMapping("/list")
    public CommonResult<CommonPage<ReissueLogisticsResponse>> selectList(@RequestBody ReissueLogisticsRequest reissueLogisticsRequest){
        return CommonResult.success(reissueLogisticsService.selectList(reissueLogisticsRequest));
    }

    @PostMapping("/updateLogistics")
    public CommonResult updateLogistics(@RequestBody ReissueLogisticsRequest reissueLogisticsRequest){
        int count = reissueLogisticsService.updateLogistics(reissueLogisticsRequest);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @PostMapping("/refuseStatus")
    public CommonResult refuseStatus(@RequestBody ReissueLogisticsUpdateRequest request){
        int count = reissueLogisticsService.refuseStatusAndReason(request.getId(),request.getRefuseReason());
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @PostMapping("/refuseListStatus")
    public CommonResult refuseListStatus(@RequestBody ReissueLogisticsUpdateRequest request){
        log.info("自动拒绝补单参数refuseReason:{}",request.getRefuseReason(), JSONObject.toJSONString(request.getReissueLogisticsIdList()));
        int count = reissueLogisticsService.refuseListStatus(request.getReissueLogisticsIdList(),request.getRefuseReason());
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

}
